package com.android.demo.widget.takeacay;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.R.layout;
import android.util.Base64;

import com.android.demo.widget.BitmapToolkit;

public class ProductSdk extends AbsSdk{
	public static final int MSG_GET_SHOP = 100;
	
	public static final int MSG_POST_SHOP = 500;
	
	public ProductSdk(){
	}
	
	public static final String FUNCTION_SHOP = "/shop";
	public static final String FUNCTION_IMAGE = "/image";
	public static final String FUNCTION_COMMENT = "/comment";
	public static final String FUNCTION_TELEPHONE = "/telephone";
	public static final String T_PRODUCT = "name";
	
	public static final String C_NAME = "name";
	public static final String C_TICK = "tick";
	public static final String C_ID = "id";
	
	public static final String C_DES = "description";
	public static final String C_IMAGE_CONTENT = "imageContent";
	public static final String C_IMAGE_EXT = "ext";
	public static final String C_IMAGE_URL = "imageUrl";
	public static final String C_PHONE_NUMBER = "number";
	public static final String C_BUSINESS_HOURS = "businessHours";
	public static final String C_ADDRESS = "address";
	public static final String C_LATITUDE = "latitude";
	public static final String C_LONGITUDE = "longitude";
	public static final String C_CREATETIME = "createTime";
	
	public static final String C_COLLECTED = "collected";

	
	public static final String C_COMMENT = "comment";
	public static final String C_STAR = "star";
	public static final String C_AUTHOR = "author";
	
	private String formatTicket(long tick){
		return "?tick="+tick;
	}
	
	/**
	 * <br>Description:增加店铺
	 * <br>Author:hexy
	 * <br>Date:2011-4-18下午11:34:05
	 * @param shopInfo
	 * @return
	 */
	public SdkResult postShop(ProductInfo shopInfo){
		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_POST);
		hw.setFunction(FUNCTION_SHOP);
		
		Map<String, String> mapShopInfo = new HashMap<String, String>();
		byte[] byteImage = null;
		if (shopInfo.imageUrl.length()>0) {
			byteImage = com.android.demo.widget.takeacay.Base64.encode(BitmapToolkit.loadLocalBitmapExactScaledBytes(shopInfo.imageUrl, 780));
			mapShopInfo.put(C_IMAGE_CONTENT, new String(byteImage));
			mapShopInfo.put(C_IMAGE_EXT, "2");
		} 
		
		mapShopInfo.put(C_NAME, shopInfo.name);
		mapShopInfo.put(C_PHONE_NUMBER, shopInfo.phone);
		mapShopInfo.put(C_DES, shopInfo.des);
//		"latitude": "24.734749",
//		"longitude": "118.63904"
		mapShopInfo.put(C_LATITUDE, String.valueOf(shopInfo.latitude));
		mapShopInfo.put(C_LONGITUDE, String.valueOf(shopInfo.longitude));
		
		hw.setJsonParamMap(mapShopInfo);
		SdkResult result =  hw.doHttpMethod();
		
		if (result.ret == 200) {
			try {
				JSONObject json = new JSONObject(result.response);
				if (json.has("image")) {
					String dir = BitmapToolkit.DIR_MYHOME_TAKEAWAY;
					String url = ProductInfo.getImageUrl(ProductInfo.getImageUrl(json.getString("image")));
					BitmapToolkit bt = new BitmapToolkit(dir, url, "", "");
					bt.saveByte(byteImage);
				}
				
			}catch (Exception e) {
			}
			
		}
		
		return result;
	}
	/**
	 * <br>Description:获取店铺列表
	 * <br>Author:hexy
	 * <br>Date:2011-4-18下午11:34:31
	 * @param tick
	 * @return
	 * / type =oldest   type=newest  pos=20 , long lati  , r  ,tick=null,
	 */
	public SdkResult getShop(long tick, String type,int pos){
		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_GET);
		hw.setFunction(FUNCTION_SHOP+formatTicket(tick)+"&pos="+pos+"&type="+type);
		return hw.doHttpMethod();
	}
	
	/**
	 * <br>Description:增加店铺评论
	 * <br>Author:hexy
	 * <br>Date:2011-4-18下午11:33:36
	 * @param commentInfo
	 * @return
	 */
//	public SdkResult postShopComment(CommentInfo commentInfo, long shopid){
//		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_POST);
//		hw.setFunction(FUNCTION_SHOP+"/"+shopid+"/comment");
//		
//		Map<String, String> mapShopInfo = new HashMap<String, String>();
//		
//		mapShopInfo.put(C_COMMENT, commentInfo.comment);
//		mapShopInfo.put(C_STAR, String.valueOf(commentInfo.star));
//		mapShopInfo.put(C_AUTHOR, commentInfo.author);
//		
//		hw.setJsonParamMap(mapShopInfo);
//		SdkResult result =  hw.doHttpMethod();
//		
//		return result;
//	}
	
	public SdkResult getShopDetail(long shopid){
		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_GET);
		hw.setFunction(FUNCTION_SHOP + "/"+shopid);
		
		SdkResult result =  hw.doHttpMethod();
		return result;
	}
//	
//	/**
//	 * <br>Description:增加店铺照片
//	 * <br>Author:hexy
//	 * <br>Date:2011-4-18下午11:33:36
//	 * @param commentInfo
//	 * @return
//	 */
//	public SdkResult postShopImage(String imageUrl , long shopid){
//		if (imageUrl==null || imageUrl.length()==0) return null;
//		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_POST);
//		hw.setFunction(FUNCTION_IMAGE+"/"+shopid+"/image");
//		
//		Map<String, String> mapShopInfo = new HashMap<String, String>();
//		byte[] byteImage = null;
//		if (imageUrl.length()>0) {
//			byteImage = Base64.encode(BitmapToolkit.loadLocalBitmapExactScaledBytes(imageUrl, 780));
//			mapShopInfo.put(C_IMAGE_CONTENT, new String(byteImage));
//			mapShopInfo.put(C_IMAGE_EXT, "2");
//		}
//		
//		hw.setJsonParamMap(mapShopInfo);
//		SdkResult result =  hw.doHttpMethod();
//		
//		if (result.ret == 200) {
//			try {
//				JSONObject json = new JSONObject(result.response);
//				if (json.has("image")) {
//					String dir = BitmapToolkit.DIR_MYHOME_TAKEAWAY;
//					String url = ProductInfo.getImageUrl(ProductInfo.getImageUrl(json.getString("image")));
//					BitmapToolkit bt = new BitmapToolkit(dir, url, "", "");
//					bt.saveByte(byteImage);
//				}
//				
//			}catch (Exception e) {
//			}
//			
//		}
//		
//		return result;
//	}
//	
//	public SdkResult getShopImage(long shopid, long tick){
//		HttpWrap hw = new HttpWrap(HttpWrap.HTTP_GET);
//		hw.setFunction(FUNCTION_IMAGE + "/"+shopid+formatTicket(tick));
//		
//		SdkResult result =  hw.doHttpMethod();
//		
//		if (result.ret == 200) {
//			try {
//				JSONObject json = new JSONObject(result.response);
//			}catch (Exception e) {
//			}
//		}
//		return result;
//	}
//	
//	添加店铺照片
//	POST /shop/<id>/image
//	{
//	"ext":"1-bmp,2-png,3-gif,4-jpg"
//	"imageContent":"base64"
//	}
//
//	GET /shop?tick=0
//	GET /image?tick=0
//	private Map<String, String> mShop;
//	private Map<String, String> mShopModi;
//	
//	public void testDatd(String exif, String imageBytes){
//		mShop = new HashMap<String, String>();
//		mShop.put(C_NAME, "name test" + exif);
//		mShop.put(C_DES, "description test");
//		
//		mShop.put(C_IMAGE_CONTENT, imageBytes);
//		mShop.put(C_IMAGE_EXT, "1");
//		
//		mShopModi = new HashMap<String, String>();
//		mShopModi.put(C_NAME, "name modi" + exif);
//		mShopModi.put(C_DES, "description modi");
//		mShopModi.put(C_IMAGE_CONTENT, "0000000000000");
//		mShopModi.put(C_IMAGE_EXT, "2");
//	}
}
